#!/usr/bash

HOST=$(shell hostname | cut -c1-7)
#HOST=lxifort
#HOST=tlsifor

CDIR     = $(shell pwd)
CDIR     = .
CDIR_SRC = $(CDIR)/src
CDIR_INC = $(CDIR)/inc
CDIR_MOD = $(CDIR)/mod
CDIR_OBJ = $(CDIR)/obj
CDIR_LIB = $(CDIR)/lib

ifeq ($(HOST),lxgmap1)
    CC       = gcc
    FC       = gfortran

    RM       = rm -f
    RANLIB   = /usr/bin/ranlib
    AR       = /usr/bin/ar

    NETCDF_DIR=/usr
    NETCDF_INC_DIR=$(NETCDF_DIR)/include
    NETCDF_LIB_DIR=$(NETCDF_DIR)/lib
    NETCDF_LIBS=-lnetcdf -lnetcdff

    INCLUDES =-I$(NETCDF_INC_DIR) -I$(CDIR_INC) -J$(CDIR_MOD) 
    #INCLUDES =-I$(CDIR_INC) -J$(CDIR_MOD) 
    CCFLAGS  =$(INCLUDES)
    FCFLAGS  =$(INCLUDES) -cpp -g -fbacktrace -m64 -Ofast -flto -march=native -funroll-loops
    LDFLAGS  =$(NETCDF_LIBS)

else ifeq ($(HOST),lxifort)
    CC       = icc
    FC       = ifort

    RM       = rm -f
    RANLIB   = /usr/bin/ranlib
    AR       = /usr/bin/ar

    NETCDF_DIR=/home/$(USER)/intel_libs
    NETCDF_INC_DIR=$(NETCDF_DIR)/include
    NETCDF_LIB_DIR=$(NETCDF_DIR)/lib
    NETCDF_LIBS=-Wl,-rpath $(NETCDF_LIB_DIR) -L$(NETCDF_LIB_DIR) -lnetcdf -lnetcdff

    INCLUDES =-I$(CDIR_INC) -I$(NETCDF_INC_DIR) -module $(CDIR_MOD) 
    CCFLAGS  =$(INCLUDES)
    #FCFLAGS  =$(INCLUDES) -cpp -g -traceback -Ofast -xHost -funroll-loops
    #FCFLAGS  =$(INCLUDES) -cpp -g -traceback -i4
    FCFLAGS  =-g -traceback -O3 -xHost -ip -no-prec-div -static-intel -fp-model precise -fPIC $(INCLUDES) 
    #FCFLAGS  =-g -traceback -O3 -xHost -static-intel $(INCLUDES) 
    LDFLAGS  =-openmp $(NETCDF_LIBS)

else ifeq ($(HOST),tlsifor)
    CC       = icc
    FC       = ifort

    RM       = rm -f
    RANLIB   = /usr/bin/ranlib
    AR       = /usr/bin/ar

    NETCDF_DIR=/opt/intel_libs/netcdf_4.3.1.1
    NETCDF_INC_DIR=$(NETCDF_DIR)/include
    NETCDF_LIB_DIR=$(NETCDF_DIR)/lib
    NETCDF_LIBS=-Wl,-rpath $(NETCDF_LIB_DIR) -L$(NETCDF_LIB_DIR) -lnetcdf -lnetcdff

    INCLUDES =-I$(CDIR_INC) -I$(NETCDF_INC_DIR) -module $(CDIR_MOD) 
    CCFLAGS  =$(INCLUDES)
    #FCFLAGS  =$(INCLUDES) -cpp -g -traceback -Ofast -xHost -funroll-loops
    #FCFLAGS  =$(INCLUDES) -cpp -g -traceback -i4
    FCFLAGS  =-g -traceback -O3 -xHost -ip -no-prec-div -static-intel -fp-model precise -fPIC -openmp -openmp-threadprivate compat -vec-report0 $(INCLUDES) 
    LDFLAGS  =-openmp $(NETCDF_LIBS)

else ifeq ($(HOST),beaufix)
    CC       = icc
    FC       = ifort

    RM       = rm -f
    RANLIB   = /usr/bin/ranlib
    AR       = /usr/bin/ar

    NETCDF_DIR=/opt/softs/libraries/ICC13.1.4.183/netcdf-4.3.0
    NETCDF_INC_DIR=$(NETCDF_DIR)/include
    NETCDF_LIB_DIR=$(NETCDF_DIR)/lib
    NETCDF_LIBS=-L$(NETCDF_LIB_DIR) -lnetcdf -lnetcdff

    INCLUDES =-I$(CDIR_INC) -I$(NETCDF_INC_DIR) -module $(CDIR_MOD) 
    CCFLAGS  =$(INCLUDES)
    #FCFLAGS  =$(INCLUDES) -g -traceback -O3 #-gen-interfaces source
    #LDFLAGS  =-openmp $(LIBS) $(NETCDF_LIBS)
    FCFLAGS  =-g -traceback -O3 -xAVX -ip -no-prec-div -static-intel -fp-model precise -fPIC -openmp -openmp-threadprivate compat -vec-report0 -finline-limit=500 $(INCLUDES) 
    LDFLAGS  =-openmp $(NETCDF_LIBS)

else ifeq ($(HOST),toulous)
    CC       = gcc
    FC       = gfortran

    RM       = rm -f
    RANLIB   = /usr/bin/ranlib
    AR       = /usr/bin/ar

    NETCDF_DIR=/usr
    NETCDF_INC_DIR=$(NETCDF_DIR)/include
    NETCDF_LIB_DIR=$(NETCDF_DIR)/lib
    NETCDF_LIBS=-L$(NETCDF_LIB_DIR) -lnetcdf -lnetcdff

    INCLUDES =-I$(CDIR_INC) -I$(NETCDF_INC_DIR) -J$(CDIR_MOD) 
    CCFLAGS  =$(INCLUDES)
    FCFLAGS  =$(INCLUDES) -cpp -g -fbacktrace -m64 -Ofast -flto -march=native -funroll-loops
    LDFLAGS  =$(NETCDF_LIBS)

endif

MOD_OBJ  = 					\
        $(CDIR_OBJ)/iasing_osse.o		\
        $(CDIR_OBJ)/iasing_osse_1dvar.o		\
	$(CDIR_OBJ)/random_numbers_mix.o 	#\
	$(CDIR_OBJ)/precision.o 		   \
	$(CDIR_OBJ)/constants.o 		   \
	$(CDIR_OBJ)/iasing_osse.o 		   \
	$(CDIR_OBJ)/parkind1.o 		       \
	$(CDIR_OBJ)/yomhook_dummy.o 		\
	$(CDIR_OBJ)/rttov_const.o 		    
	
MOD_MOD  = 					\
	$(CDIR_MOD)/iasing_osse.mod 		\
	$(CDIR_MOD)/iasing_osse_1dvar.mod 		\
	$(CDIR_MOD)/random_numbers_mix.mod 	#\
	$(CDIR_MOD)/precision.mod		   \
	$(CDIR_MOD)/constants.mod		   \
	$(CDIR_MOD)/yomhook_dummy.mod 	    	\
	$(CDIR_MOD)/parkind1.mod 		       \
	$(CDIR_MOD)/rttov_const.mod 	
	
LIB_OBJ  = 					\
	$(CDIR_OBJ)/abor1.o 	\
	$(CDIR_OBJ)/r4a_idscreator.o 	\
	$(CDIR_OBJ)/r4a_initializer.o 	\
	$(CDIR_OBJ)/r4a_reader.o 		\
	$(CDIR_OBJ)/r4a_writer.o 		\
	$(CDIR_OBJ)/r60_idscreator.o 	\
	$(CDIR_OBJ)/r60_initializer.o 	\
	$(CDIR_OBJ)/r60_reader.o 		\
	$(CDIR_OBJ)/r60_writer.o 		\
	$(CDIR_OBJ)/r60tor51.o 		\
	$(CDIR_OBJ)/r51_idscreator.o 	\
	$(CDIR_OBJ)/r51_initializer.o 	\
	$(CDIR_OBJ)/r51_reader.o 		\
	$(CDIR_OBJ)/r51_writer.o 		\
	$(CDIR_OBJ)/checknc.o 			\
	$(CDIR_OBJ)/dichotom_i.o		\
	$(CDIR_OBJ)/dichotom_f.o        	\
	$(CDIR_OBJ)/dichotom_d.o        	\
	$(CDIR_OBJ)/yyyymmdd2array.o		\
	$(CDIR_OBJ)/rtSurfType.o 		\
	$(CDIR_OBJ)/aaaa_surfemis.o 		\
	$(CDIR_OBJ)/rad2brt_f.o 		\
	$(CDIR_OBJ)/brt2rad_f.o 		\
	$(CDIR_OBJ)/haversine_f.o 		\
	$(CDIR_OBJ)/tbrillans_f.o	   	\
	$(CDIR_OBJ)/tbrillans_d.o	   	\
	$(CDIR_OBJ)/getNumLines.o		\
	$(CDIR_OBJ)/interpolProf_f.o 	\
	$(CDIR_OBJ)/histogram_f.o 	\
	$(CDIR_OBJ)/rttov_q2v.o         	\
	$(CDIR_OBJ)/rttov_v2q.o         	#\
	$(CDIR_OBJ)/db_initializer.o     	\
	$(CDIR_OBJ)/db4a_initializer.o     	\
	$(CDIR_OBJ)/db4a_getRecord.o     	\
	$(CDIR_OBJ)/db4a_writeRecord.o     	\
	$(CDIR_OBJ)/db4a_newNCcontainer.o	\
	$(CDIR_OBJ)/db4a_writeRecordNC.o     	\
	$(CDIR_OBJ)/dbObs_initializer.o     	\
	$(CDIR_OBJ)/dbObs_getRecord.o     	\
	$(CDIR_OBJ)/proc_ChannelSelection.o	\
	$(CDIR_OBJ)/proc_Rmatrix.o		\
	$(CDIR_OBJ)/proc_Bmatrix.o		\
	$(CDIR_OBJ)/proc_1DVarSimul.o		\
	$(CDIR_OBJ)/proc_ChangeUnits_q2v.o	\
	$(CDIR_OBJ)/proc_ChangeUnitsHum_q2v.o	\
	$(CDIR_OBJ)/proc_ChangeUnitsO3_q2v.o	\
	$(CDIR_OBJ)/interpolLin_f.o		\
	$(CDIR_OBJ)/interpolLin_d.o		\
	$(CDIR_OBJ)/write_bgHeader.o	 	\
	$(CDIR_OBJ)/write_bgData.o	 	\
	$(CDIR_OBJ)/write_obsIAheader.o  	\
	$(CDIR_OBJ)/write_obsNGheader.o  	\
	$(CDIR_OBJ)/write_obsData.o 		\
	$(CDIR_OBJ)/read_NCsimul.o       	\
	$(CDIR_OBJ)/read_NCobs.o               	\
	$(CDIR_OBJ)/abor1.o       		\
	$(CDIR_OBJ)/addNoiseToHum.o 		\
	$(CDIR_OBJ)/loadProfiles.o      	\
	$(CDIR_OBJ)/loadSpectra.o           


# Library objects
LIB  = libiasing_osse.a
LIBS = -L$(CDIR_LIB) -liasing_osse 


# Recipes
$(LIB): $(MOD_OBJ) $(LIB_OBJ)
	$(AR) ru $(CDIR_LIB)/$(LIB) $^
	$(RANLIB) $(CDIR_LIB)/$(LIB)
	@echo $(HOST)

all: $(LIB) binaries

binaries: reader.exe \
	reader_51.exe \
	nc_reducer.exe \
	nc_reducer_51.exe \
	noiseadder2.exe \
	case_selector.exe \
	SimObsComp.exe \
	rad2brt.exe

clean-binaries: clean-reader \
	clean-reader_51 \
	clean-nc_reducer \
	clean-nc_reducer_51 \
	clean-noiseadder2 \
	clean-case_selector \
	clean-SimObsComp \
	clean-rad2brt

reader.exe: obj/main_reader.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-reader:
	$(RM) reader.exe obj/main_reader.o


reader_51.exe: obj/main_reader_51.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-reader:
	$(RM) reader_51.exe obj/main_reader_51.o


case_selector.exe: obj/main_case_selector.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-case_selector:
	$(RM) case_selector.exe obj/main_case_selector.o
	
noiseadder2.exe: obj/main_noiseadder2.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-noiseadder2:
	$(RM) noiseadder2.exe obj/main_noiseadder2.o
	
nc_reducer.exe: obj/main_nc_reducer.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-nc_reducer:
	$(RM) nc_reducer.exe obj/main_nc_reducer.o

nc_reducer_51.exe: obj/main_nc_reducer_51.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-nc_reducer_51:
	$(RM) nc_reducer_51.exe obj/main_nc_reducer_51.o

SimObsComp.exe: obj/main_SimObsComp.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-SimObsComp:
	$(RM) SimObsComp.exe obj/main_SimObsComp.o

rad2brt.exe: obj/main_rad2brt.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-rad2brt:
	$(RM) rad2brt.exe obj/main_rad2brt.o

fixNoRTsim.exe: obj/main_fixNoRTsim.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-fixNoRTsim:
	$(RM) fixNoRTsim.exe obj/main_fixNoRTsim.o

CopyObs.exe: obj/main_CopyObs.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-CopyObs:
	$(RM) CopyObs.exe obj/main_CopyObs.o

eraseSims.exe: obj/main_eraseSims.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-eraseSims:
	$(RM) eraseSims.exe obj/main_eraseSims.o

Create4Adb.exe: obj/main_Create4Adb.o
	@echo
	$(FC) -o $@ $< $(LDFLAGS) $(LIBS)
	@echo

clean-Create4Adb:
	$(RM) Create4Adb.exe obj/main_Create4Adb.o

# General building recipes
%.o %.mod: 
$(CDIR_OBJ)/%.o $(CDIR_MOD)/%.mod: $(CDIR_SRC)/%.f90
	$(FC) $< -o $@ -c $(FCFLAGS)
	
%.o: 
$(CDIR_OBJ)/%.o: $(CDIR_SRC)/%.c
	$(CC) $< -o $@ -c $(CCFLAGS)

%.o: 
$(CDIR_OBJ)/%.o: $(CDIR_SRC)/%.f90
	$(FC) $< -o $@ -c $(FCFLAGS)

%.o: 
$(CDIR_OBJ)/%.o: $(CDIR_SRC)/%.F90
	$(FC) $< -o $@ -c $(FCFLAGS)


# Cleaning recipes
distclean: clean-lib \
	clean-case_selector \
	clean-nc_reducer \
	clean-reader \
	clean-noiseadder2 \
	clean-Create4Adb \
	clean-fixNoRTsim \
	clean-rad2brt \
	clean-SimObsComp \
	clean-CopyObs \
	clean-eraseSims
	
clean-lib:
	$(RM) $(MOD_OBJ) $(MOD_MOD) $(LIB_OBJ) $(LIB) $(CDIR_LIB)/$(LIB) 

# making backup of the project
archive:
	rm -f libiasing_osse2.tar
	tar --exclude='*.exe' --exclude='*.o' --exclude='*.mod' --exclude='*.a' --exclude 'data' --exclude 'selected*' --exclude '*.nc' --exclude 'fort.*' --exclude 'datos_lmd' --exclude 'co2.*' --exclude '*.sh' --exclude 'res' -cf libiasing_osse2.tar *
